<?php

namespace App\Admin\Controllers\Agents;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Auth\Permission;
use App\Admin\Repositories\AgentAdminRole as Role;
use App\Models\AgentAdminRole as RoleModel;
use Dcat\Admin\Show;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Widgets\Tree;
use Dcat\Admin\Http\Controllers\AdminController;

class RoleController extends AdminController
{
    public function title()
    {
        return '代理角色';
    }

    protected function grid()
    {
        return new Grid(new Role(), function (Grid $grid) {
            // $grid->column('id', 'ID')->sortable();
            $grid->column('slug')->label('primary');
            $grid->column('name');

            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            // $grid->disableEditButton();
            $grid->disableCreateButton();
            // $grid->showQuickEditButton();
            // $grid->quickSearch(['id', 'name', 'slug']);
            // $grid->enableDialogCreate();
            $grid->disableBatchActions();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    protected function detail($id)
    {
        return Show::make($id, new Role('permissions'), function (Show $show) {
            $show->field('id');
            $show->field('slug');
            $show->field('name');

            $show->field('permissions')->unescape()->as(function ($permission) {
                $permissionModel = config('admin.database.permissions_model');
                $permissionModel = new $permissionModel();
                $nodes = $permissionModel->allNodes();

                $tree = Tree::make($nodes);

                $keyName = $permissionModel->getKeyName();
                $tree->check(
                    array_column(Helper::array($permission), $keyName)
                );

                return $tree->render();
            });

            $show->field('created_at');
            $show->field('updated_at');

            if ($show->getKey() == RoleModel::ADMINISTRATOR_ID) {
                $show->disableDeleteButton();
            }
        });
    }

    public function form()
    {
        return Form::make(Role::with(['permissions']), function (Form $form) {
            $roleTable = config('agent-admin.database.roles_table');
            $connection = config('agent-admin.database.connection');

            $id = $form->getKey();

            // $form->display('id', 'ID');

            // $form->text('slug', trans('admin.slug'))
            //     ->required()
            //     ->creationRules(['required', "unique:{$connection}.{$roleTable}"])
            //     ->updateRules(['required', "unique:{$connection}.{$roleTable},slug,$id"]);

            // $form->text('name', trans('admin.name'))->required();

            $form->display('slug', trans('admin.slug'));
            $form->display('name', trans('admin.name'));
            $form->tree('permissions')
                ->nodes(function () {
                    $permissionModel = config('agent-admin.database.permissions_model');
                    $permissionModel = new $permissionModel();

                    return $permissionModel->allNodes();
                })
                ->customFormat(function ($v) {
                    if (! $v) {
                        return [];
                    }

                    return array_column($v, 'id');
                });

            // $form->display('created_at', trans('admin.created_at'));
            // $form->display('updated_at', trans('admin.updated_at'));

            $form->disableDeleteButton();
        });
    }

    public function destroy($id)
    {
        Permission::error();
        // if (in_array(RoleModel::ADMINISTRATOR_ID, Helper::array($id))) {
        //     Permission::error();
        // }

        return parent::destroy($id);
    }
}
